﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CeA.Reports.Atendimento.Web.Helpers;
using CeA.Reports.Atendimento.DataAccess.Criteria;
using CeA.Reports.Atendimento.DataAccess.DTO;
using System.Collections.Generic;

namespace CeA.Reports.Atendimento.Web.admin {
    public partial class rfscadastro : System.Web.UI.Page {
        protected void Page_Init(object sender, EventArgs e)        {
            cmdSalvar.Click += new EventHandler(cmdSalvar_Click);
        }

        
        protected void Page_Load(object sender, EventArgs e)        {
            if (PreviousPage != null) {
                this.CodigoRfs = PreviousPage.CodigoRfs;
                carregar();
            }
        }


        protected void cmdVoltar_Click(object sender, EventArgs e)  {
            NavigationHelper.RefererRedirect("/admin/rfs.aspx");
        }


        protected void cmdSalvar_Click(object sender, EventArgs e)  {
            if (this.CodigoRfs > 0)
                atualizar();
            else
                inserir();
        }


        private void carregar()                                     {
            int rfsId = this.CodigoRfs;
            if (rfsId > 0) {
                RfsCriteria filter = new RfsCriteria();
                filter.addRfs(rfsId);

                List<RfsDTO> ds = Qtech.Reports.Atendimento.Services.CadastroFacade.FindRfs(filter);

                RfsDTO dto = ds[0];
                txtNome.Text = dto.Nome;
                txtDescricao.Text = dto.Descricao;
                CodigoRfs = dto.Id;
            }
        }


        private void inserir()                                      {
            RfsDTO dto = new RfsDTO();
            dto.Nome = txtNome.Text;
            dto.Descricao = txtDescricao.Text;

            dto = Qtech.Reports.Atendimento.Services.CadastroFacade.SaveRfs(dto);

            this.CodigoRfs = dto.Id;

            MessageBox.pop("Registro inserido com sucesso!");
        }


        private void atualizar()                                    {
            RfsDTO dto = new RfsDTO();
            dto.Id = this.CodigoRfs;
            dto.Nome = txtNome.Text;
            dto.Descricao = txtDescricao.Text;

            Qtech.Reports.Atendimento.Services.CadastroFacade.UpdateRfs(dto);

            MessageBox.pop("Registro atualizado com sucesso!");
        }


        public int CodigoRfs                                        {
            get {
                if (ViewState["m_codigoRfs"] != null)
                    return Convert.ToInt32(ViewState["m_codigoRfs"]);
                return 0;
            }
            set {
                ViewState.Add("m_codigoRfs", value);
            }
        }
    }
}
